VERSION 5.00
Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "COMDLG32.OCX"
Begin VB.Form MMI 
   BackColor       =   &H00FFFFFF&
   Caption         =   "MMI"
   ClientHeight    =   2625
   ClientLeft      =   1830
   ClientTop       =   1590
   ClientWidth     =   5355
   Icon            =   "f1 MMI.frx":0000
   ScaleHeight     =   175
   ScaleMode       =   3  'Pixel
   ScaleWidth      =   357
   Begin MSComDlg.CommonDialog fsel 
      Left            =   0
      Top             =   1560
      _ExtentX        =   847
      _ExtentY        =   847
      _Version        =   393216
   End
   Begin VB.Timer PaceMaker 
      Enabled         =   0   'False
      Left            =   0
      Top             =   1080
   End
   Begin VB.Shape RubBnd 
      Height          =   135
      Left            =   600
      Top             =   1200
      Visible         =   0   'False
      Width           =   135
   End
   Begin VB.Label Led 
      Alignment       =   2  'Center
      Appearance      =   0  'Flat
      BackColor       =   &H80000005&
      BorderStyle     =   1  'Fixed Single
      ForeColor       =   &H80000008&
      Height          =   135
      Left            =   0
      TabIndex        =   3
      ToolTipText     =   "Click to start/stop DDE links"
      Top             =   0
      Width           =   135
   End
   Begin VB.Label Desc 
      Appearance      =   0  'Flat
      AutoSize        =   -1  'True
      BackColor       =   &H80000005&
      BackStyle       =   0  'Transparent
      Caption         =   "Desc(0) - Desc obj."
      ForeColor       =   &H80000008&
      Height          =   195
      Index           =   0
      Left            =   1560
      TabIndex        =   2
      Top             =   0
      Visible         =   0   'False
      Width           =   1365
   End
   Begin VB.Label Disp 
      Appearance      =   0  'Flat
      BackColor       =   &H80000005&
      BorderStyle     =   1  'Fixed Single
      Caption         =   "Disp(0) - Display obj."
      ForeColor       =   &H80000008&
      Height          =   255
      Index           =   0
      Left            =   0
      TabIndex        =   1
      Top             =   0
      Visible         =   0   'False
      Width           =   1575
   End
   Begin VB.Label Link 
      Appearance      =   0  'Flat
      BackColor       =   &H80000005&
      BorderStyle     =   1  'Fixed Single
      Caption         =   "Link(0) - Link obj."
      ForeColor       =   &H80000008&
      Height          =   255
      Index           =   0
      Left            =   0
      TabIndex        =   0
      Top             =   240
      Visible         =   0   'False
      Width           =   1575
   End
   Begin VB.Menu mnuObj 
      Caption         =   "Object Menu"
      Visible         =   0   'False
      Begin VB.Menu mnuObj_ 
         Caption         =   "Object Settings"
         Index           =   0
      End
      Begin VB.Menu mnuObj_ 
         Caption         =   "Duplicate"
         Index           =   1
      End
      Begin VB.Menu mnuObj_ 
         Caption         =   "Delete"
         Index           =   2
      End
      Begin VB.Menu mnuObj_ 
         Caption         =   "Bring to front"
         Index           =   3
      End
      Begin VB.Menu mnuObj_ 
         Caption         =   "Send to back"
         Index           =   4
      End
   End
   Begin VB.Menu mnuFrm 
      Caption         =   "Form Menu"
      Visible         =   0   'False
      Begin VB.Menu mnuFrm_ 
         Caption         =   "Start Links"
         Index           =   1
      End
      Begin VB.Menu mnuFrm_ 
         Caption         =   "Write Enable"
         Enabled         =   0   'False
         Index           =   2
      End
      Begin VB.Menu mnuFrm_ 
         Caption         =   "-"
         Index           =   10
      End
      Begin VB.Menu mnuFrm_ 
         Caption         =   "New"
         Index           =   11
      End
      Begin VB.Menu mnuFrm_ 
         Caption         =   "Save"
         Index           =   12
      End
      Begin VB.Menu mnuFrm_ 
         Caption         =   "Save as"
         Index           =   13
      End
      Begin VB.Menu mnuFrm_ 
         Caption         =   "Load"
         Index           =   14
      End
      Begin VB.Menu mnuFrm_ 
         Caption         =   "-"
         Index           =   20
      End
      Begin VB.Menu mnuFrm_ 
         Caption         =   "Settings"
         Index           =   21
      End
      Begin VB.Menu mnuFrm_ 
         Caption         =   "Info"
         Index           =   22
      End
      Begin VB.Menu mnuFrm_ 
         Caption         =   "-"
         Index           =   30
      End
      Begin VB.Menu mnuFrm_ 
         Caption         =   "Exit"
         Index           =   31
      End
   End
End
Attribute VB_Name = "MMI"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
DefInt A-Z

'For OnTop
Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, _
    ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal y As Long, _
    ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
'For OnTop
Private Const HWND_TOPMOST = -1
Private Const HWND_NOTOPMOST = -2
Private Const SWP_SHOWWINDOW = &H40
Private Const SWP_NOMOVE = &H2
Private Const SWP_NOOWNERZORDER = &H200      '  Don't do owner Z ordering
Private Const SWP_NOSIZE = &H1


'For moving and resizing
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Type POINTAPI
        X As Long
        y As Long
End Type

Private Sub Form_Load()

    '1  Setup Form
        MMI.ScaleMode = SCLPIXEL
    
    '2  Initalize Layout Database (LAY) and autoload if selected
        LAYCtrl.aReset
        LAYCtrl.aLoad App.Path & "/" & App.EXEName & ".lay"
        If Not (LAY.AutoLoad = 1) Then LAYCtrl.aReset
        ReDrawLayout
        Me.Show
    
    '3  Initalize Obj Database (VDT)
        VDTCtrl.Initalize
        If LAY.AutoLoad Then VDTCtrl.LayoutLoad
        
    '4  Setup File Selector
        fsel.InitDir = App.Path
        fsel.Filter = "Layout files (*.lay)|*.lay|All files (*.*)|*.*"
        fsel.DefaultExt = "lay"
    
    '5 Set the update cycle time
        PaceMaker.Interval = 1000   '1 sec
        PaceMaker.Enabled = True
        
    '6  Start links if set to autostart
        If LAY.AutoStart = 1 Then MMI_LinksStart
        
        
End Sub

'UNLOAD FORM
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    If LAY.AutoSave Then
        LAYCtrl.aSave
        VDTCtrl.LayoutSave
    End If
End Sub

'PACEMAKER TIMER
Private Sub PaceMaker_Timer()
        If Vdt.LinkRun Then
            LinkUD.ddeLinkUpDate
            If Led.BackColor = vbGreen Then
                Led.BackColor = vbWhite
            Else
                Led.BackColor = vbGreen
            End If
        Else
            Led.BackColor = vbWhite
        End If
End Sub

'-----------------------------------
'   MOUSE ON DISPLAY OBJECT
'-----------------------------------
Private Sub Disp_MouseDown(index As Integer, Button As Integer, Shift As Integer, X As Single, y As Single)
    If Not Vdt.LinkRun Or Not Vdt.ModeRun Then
        Select Case Button
            Case vbLeftButton
                Call DISPmove(Disp(index), True)
            Case vbRightButton
                Vdt.index = index
                PopupMenu mnuObj
        End Select
    End If
End Sub
Private Sub Disp_MouseMove(index As Integer, Button As Integer, Shift As Integer, X As Single, y As Single)
    If Not Vdt.LinkRun Or Not Vdt.ModeRun Then
        If Button = vbLeftButton Then Call DISPmove(Disp(index), False)
    End If
End Sub

Private Sub Disp_Click(index As Integer)
    If Vdt.LinkRun And Vdt.ModeRun And Vdt.Item(index).Writable Then
        Vdt.index = index
        MMIWriteVal.Show
    End If
End Sub

Private Sub Disp_DblClick(index As Integer)
    If Not Vdt.LinkRun Or Not Vdt.ModeRun Then
        Vdt.index = index
        mnuObj__Click 0
    End If
End Sub

Private Sub DISPmove(movOBJ As Object, mDown As Boolean)
    Static Xos As Integer
    Static Yos As Integer
    Static XosToDesc As Integer
    Static YosToDesc As Integer
    Dim mousePT As POINTAPI
    Call GetCursorPos(mousePT)
    If mDown Then
        Xos = mousePT.X - movOBJ.Left
        Yos = mousePT.y - movOBJ.Top
        XosToDesc = Desc(movOBJ.index).Left - movOBJ.Left
        YosToDesc = Desc(movOBJ.index).Top - movOBJ.Top
    Else
        If LAY.Snap Then
            movOBJ.Left = Int((mousePT.X - Xos) / LAY.SnapX) * LAY.SnapX
            movOBJ.Top = Int((mousePT.y - Yos) / LAY.SnapY) * LAY.SnapY
        Else
            movOBJ.Left = (mousePT.X - Xos)
            movOBJ.Top = (mousePT.y - Yos)
        End If
        Desc(movOBJ.index).Left = movOBJ.Left + XosToDesc
        Desc(movOBJ.index).Top = movOBJ.Top + YosToDesc
    End If
End Sub

'-----------------------------------
'   MOUSE ON THE FORM (MMI)
'-----------------------------------
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, y As Single)
    If Button = vbRightButton Then Me.PopupMenu mnuFrm
    If Button = vbLeftButton And Not Vdt.ModeRun Then drawSQ 1, X, y
End Sub

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, y As Single)
    If Button = vbLeftButton And Not Vdt.ModeRun Then drawSQ 2, X, y
End Sub

Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, y As Single)
    drawSQ 3, X, y
End Sub

Private Sub drawSQ(MouseStep As Integer, X As Single, y As Single)
'Rubber Band / ReSizer Routine
    Const Xmin = 5 'min sq size
    Const Ymin = 5 'min sq size
    Static Xos As Integer 'offset between mouse and form
    Static Yos As Integer 'offset between mouse and form
    Static Xs As Integer 'X at start
    Static Ys As Integer 'Y at start
    Dim M As POINTAPI: Call GetCursorPos(M) 'get the mouse pos
    With RubBnd
        Select Case MouseStep
            Case 1 'MouseDown
                Xs = X: Ys = y: Xos = M.X - Xs: Yos = M.y - Ys
                .Left = M.X - Xos
                .Top = M.y - Yos
                .Width = Xmin
                .Height = Ymin
                .Visible = True
            Case 2 'Mouse Move
                If M.X - Xos > Xs Then .Width = M.X - Xos - .Left Else .Left = M.X - Xos: .Width = Xs - .Left
                If M.y - Yos > Ys Then .Height = M.y - Yos - .Top Else .Top = M.y - Yos: .Height = Ys - .Top
            Case 3 'Mouse Up
                .Visible = False
        End Select
    End With
End Sub

'-----------------------------------
'   MOUSE ON THE DESC OBJ
'-----------------------------------
Private Sub Desc_MouseDown(index As Integer, Button As Integer, Shift As Integer, X As Single, y As Single)
    If Button = vbLeftButton Then
        Call DESCmove(Desc(index), True)
    End If
End Sub

Private Sub Desc_MouseMove(index As Integer, Button As Integer, Shift As Integer, X As Single, y As Single)
    If Button = vbLeftButton Then
        Call DESCmove(Desc(index), False)
    End If
End Sub

Private Sub DESCmove(movOBJ As Object, mDown As Boolean)
    Static Xos As Integer
    Static Yos As Integer
    Dim mousePT As POINTAPI
    Call GetCursorPos(mousePT)
    If mDown Then
        Xos = mousePT.X - movOBJ.Left
        Yos = mousePT.y - movOBJ.Top
    Else
        If LAY.Snap Then
            movOBJ.Left = Int((mousePT.X - Xos) / LAY.SnapX) * LAY.SnapX + LAY.DescXos
            movOBJ.Top = Int((mousePT.y - Yos) / LAY.SnapY) * LAY.SnapY + LAY.DescYos
        Else
            movOBJ.Left = (mousePT.X - Xos)
            movOBJ.Top = (mousePT.y - Yos)
        End If
    End If
End Sub

'------------------------------
'   MENUS
'------------------------------
Private Sub mnuFrm__Click(index As Integer)
    Select Case index

        Case 1 'Start/Stop Links
            If Vdt.LinkRun Then
                MMI_LinksStop
            Else
                MMI_LinksStart
            End If

        Case 2 'Write/Edit Mode
            If Vdt.ModeRun Then
                MMI_WriteDisable
            Else
                MMI_WriteEnable
            End If
        '- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
        
        Case 11 'New
            MMI_LinksStop
            LAYCtrl.aReset
            VDTCtrl.Initalize
            ReDrawLayout
        
        Case 12 'Save
                LAYCtrl.aSave
                VDTCtrl.LayoutSave
        
        Case 13 'Save As
            fsel.ShowSave
            If fsel.FileName <> "" Then
                LAY.LayFileName = fsel.FileName
                LAY.ObjFileName = Left(LAY.LayFileName, Len(LAY.LayFileName) - 4) & ".obj"
                LAYCtrl.aSave
                VDTCtrl.LayoutSave
            End If
        
        Case 14 'Load
            fsel.ShowOpen
            If fsel.FileName <> "" Then
                MMI_LinksStop
                LAYCtrl.aLoad fsel.FileName
                ReDrawLayout
                VDTCtrl.Initalize
                VDTCtrl.LayoutLoad
            End If
        '- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
        
        Case 21 'settings
            MMIFrmSet.Show vbModal, Me ' settings
        
        Case 22  'Show LAY
            LAYCtrl.aShow
        '- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
        
        Case 31  'Exit
            Unload Me
    
    End Select
End Sub
'Process clicking the LED PB's
        
Private Sub sparecode()
  '      Case 2 'Search Replace
            Dim pT As Integer
            Dim newTopic As String
            Led(2).BackColor = vbGreen
            newTopic = InputBox("Enter new DDE Topic", LAY.Title & " - Change DDE Topic")
            If newTopic <> "" Then
                For pT = 0 To Vdt.Count
                    With Vdt.Link(pT)
                        .LinkTopic = Left(.LinkTopic, InStr(.LinkTopic, "|")) & newTopic
                    End With
                    VDTCtrl.VdtUpDateToolTip pT
                Next
                'Restart links
                If Vdt.LinkRun Then LinkUD.ddeLinksStart
            End If
            Led(2).BackColor = vbWhite
        
 '       Case 3 'On Top
            Const winFlags = SWP_SHOWWINDOW + SWP_NOMOVE + SWP_NOSIZE
            If Led(3).BackColor <> vbGreen Then
                SetWindowPos Me.hwnd, HWND_TOPMOST, 0, 0, 0, 0, winFlags
                Led(3).BackColor = vbGreen
            Else
                SetWindowPos Me.hwnd, HWND_NOTOPMOST, 0, 0, 0, 0, winFlags
                Led(3).BackColor = vbWhite
            End If
'        Case 4 'Layout to notepad
            Shell "notepad.exe " & LAY.ObjFileName, vbNormalFocus
    

End Sub

Private Sub mnuObj__Click(index As Integer)
    Select Case index
        Case 0: MMIset.Show vbModal, Me ' settings
        Case 1: objClone    ' duplicate
        Case 2: objDelete ' delete
        Case 3: objToFront ' to front
        Case 4: objToBack ' to back
    End Select
    Vdt.index = -1
End Sub

'Restore Layout to the MMI form
Private Sub ReDrawLayout()
    MMI.Caption = LAY.Title
    MMI.BackColor = LAY.BackColor
    Select Case LAY.WinState
        Case 0 'Normal
            MMI.WindowState = WINNORM
            MMI.Left = LAY.Left
            MMI.Top = LAY.Top
            MMI.Width = LAY.Width
            MMI.Height = LAY.Height
        Case 1 'Min
            MMI.WindowState = WINNORM
            MMI.Left = Screen.Width * 0.1
            MMI.Top = Screen.Height * 0.1
            MMI.Width = Screen.Width * 0.7
            MMI.Height = Screen.Height * 0.7
        Case 2 'Max
            MMI.WindowState = WINNORM
            MMI.Left = Screen.Width * 0.1
            MMI.Top = Screen.Height * 0.1
            MMI.Width = Screen.Width * 0.7
            MMI.Height = Screen.Height * 0.7
            MMI.WindowState = WINMAX
    End Select
End Sub

Sub MMI_LinksStart()
    'Do all the things needed to start the ddelinks
    mnuFrm_(1).Caption = "Stop Links"
    LinkUD.ddeLinksStart
    Vdt.LinkRun = True
    mnuFrm_(2).Enabled = True
End Sub

Sub MMI_LinksStop()
    'Do all the things needed to stop the ddelinks
    LinkUD.ddeLinksStop
    Vdt.LinkRun = False
    mnuFrm_(1).Caption = "Start Links"
    MMI_WriteDisable
    mnuFrm_(2).Enabled = False
End Sub

Sub MMI_WriteEnable()
    'Do all the things needed to goto Write Enable Mode
    mnuFrm_(2).Caption = "Write Disable"
    Vdt.ModeRun = True
    Me.Caption = Me.Caption & " [WRITE MODE]"
End Sub

Sub MMI_WriteDisable()
    'Do all the things needed to goto Write Disabled Mode
    mnuFrm_(2).Caption = "Write Enable"
    Vdt.ModeRun = False
    If Right(Me.Caption, 5) = "MODE]" Then Me.Caption = Left(Me.Caption, Len(Me.Caption) - Len(" [WRITE MODE]"))
End Sub
